package code.structure.module.scene.view.iso
{
	import code.structure.manager.loader.DllLoaderManager;
	import code.structure.manager.door.DoorMaterialData;
	import code.structure.manager.door.DoorMaterialManager;
	import code.structure.module.scene.view.YIsoWorld;
	import code.untils.SceneConfig;
	
	import com.patience.map.isometric.core.IsoWorld;

	/**
	 *门 
	 * @author Yuri
	 * 
	 */
	public class DoorIsoObject extends DecorateIsoObject
	{
		/**
		 *零部件分拆 
		 */
		private var _componentList:Array;
		
		private var _doorMaterialData:DoorMaterialData;
		public function DoorIsoObject()
		{
			super();
			isWalk = true;
			gridIndex = -5;
			_componentList = [];
		}
		
		/**
		 *刷新素材 
		 * 
		 */
		override protected function refreshMaterial(path:String,subKey:String):void{
			isMaterialLoadComplete = false;
			DllLoaderManager.getInstance().getMaterial(path,subKey,onDllgetMaterialHandler);
		}
		
		protected function onDllgetMaterialHandler(c:Class,key:String=""):void{
			DoorMaterialManager.getInstance().getDoorMaterialData(_data.data.id+"_scene",c,onChangeBitmapComplete);
		}
		
		private function onChangeBitmapComplete(t:DoorMaterialData):void
		{
			if(!t) return;
			if(!_mainBitMapContainer) return;
			isMaterialLoadComplete = true;
			createBody();
			if(!bitmap) return;
			doorMaterialData = t;
			bitmap.bitmapData = t.doorBitmapList[2];
			bitmap.x = t.doorBitmapList[0];
			bitmap.y = t.doorBitmapList[1];
			createComponent();
			if(parentWorld && this.parent){
				showComponent((parentWorld as YIsoWorld).status == SceneConfig.SCENE_NORMAL,true); 
			}
		}

		public function set doorMaterialData(value:DoorMaterialData):void
		{
			_doorMaterialData = value;
		}
		
		/**
		 *创建零件 
		 * 
		 */
		private function createComponent():void{
			if(!_doorMaterialData) return;
			if(_componentList.length==0){
				var count:int = _doorMaterialData.componentBitMapList.length;
				for(var i:int=0;i<count;i++){
					var iso:DoorComponentIsoObject = new DoorComponentIsoObject(i);
					iso.materialInfo = _doorMaterialData.componentBitMapList[i];
					iso.size = size;
					iso.parentDoor = this;
					if(i==0){
						iso.isWalk = true;
					}else{
						iso.gridIndex = 100;
					}
					
					iso.isUnlock = this.isUnlock;
					_componentList.push(iso);
				}
			}
		}

		/**
		 *是否已经显示组件 
		 */
		private var _isShowComponent:Boolean;
		
		/**
		 * 是否显示组件 
		 * @param isShow
		 * 
		 */
		public function showComponent(isShow:Boolean=true,isNeedSort:Boolean = true):void{
			if(!_isMaterialLoadComplete || _isShowComponent == isShow) return;
			var count:int = _componentList.length;
			var i:int = 0;
			var tempIsoWorld:IsoWorld;
			for(i=0;i<count;i++){
				var iso:DoorComponentIsoObject = _componentList[i] as DoorComponentIsoObject;
				if(isShow){
					if(!tempIsoWorld){
						tempIsoWorld = parentWorld;
					}
					iso.direction = direction;
					tempIsoWorld.addToWorld(iso);
					if(isNeedSort) (tempIsoWorld as YIsoWorld).sortIsoObjectByAreaDistance(iso,5);
					
				}else{
					if(!tempIsoWorld){
						tempIsoWorld = iso.parentWorld;
					}
					tempIsoWorld.removeFromWorld(iso,false);
				}
			}
			if(!isShow){
				if(!this.parentWorld){
					tempIsoWorld.addToWorld(this);
					(tempIsoWorld as YIsoWorld).sortIsoObjectByAreaDistance(this,5);
				}
					
			}else{
				if(this.parentWorld)
				tempIsoWorld.removeFromWorld(this,false);
			}
			tempIsoWorld = null;
			_isShowComponent = isShow;
		}
		
		/**
		 *清除零部件 
		 * 
		 */
		private function clealComponent():void{
			for each(var iso:DoorComponentIsoObject in _componentList){
				if(!iso) continue;
				if(iso.parentWorld){
					iso.parentWorld.removeFromWorld(iso);
				}else{
					iso.destroy();
					iso = null;
				}
			}
			_componentList = [];
		}
		
		override public function destroy():void{
			clealComponent();
			super.destroy();
		}
	}
}